/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sql;

import java.sql.*;
import javax.swing.JOptionPane;
import negocio.*;

import java.util.ArrayList;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;


/**
 *
 * @author Alumno
 */
public class Conexion {
    
    private Connection con;
    private Statement st;
    private DefaultTableModel dt;
    String[] apellido = null;
    
    
    
    public void conectar()
    {
        try
            {
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sia2", "root", null);
                st = con.createStatement();
            }
        catch(Exception ex)
            {
                ex.printStackTrace();
            }
    }
    
    public void insertar(Persona persona)
    {
        try
            {
                conectar();
               st.executeUpdate("INSERT INTO persona "
                                + "VALUES(" +persona.getRut()+","
                                + "'"+persona.getRutVerificador()+"',"
                                + "'"+persona.getNombre()+"',"
                                + "'"+persona.getApellidoPaterno()+"',"
                                + "'"+persona.getApellidoMaterno()+"',"
                                + ""+persona.getSueldoBase()+","
                                + ""+persona.getCargaFamiliar()+","
                                + ""+persona.getMovilizacion()+","
                                + ""+persona.getColacion()+","
                                + "'"+persona.getFecha()+"');");
                con.close();
            }
        catch(Exception ex)
            {
                ex.getStackTrace();
            }
    }
    

public boolean eliminar(String rut)
    {
        int verificar = 0;
        try
            {
                conectar();
                verificar = st.executeUpdate("delete from persona where rut ="+rut+";");
                con.close();
            }
        catch(Exception ex)
            {
                ex.getStackTrace();
            }
        if (verificar!=0)
            {
                return true;
            }
        else
            {
                return false;
            }
    }
    
public boolean modificar(Persona persona)
{
    int verifica=0;
    try
        {
            conectar();
            verifica = st.executeUpdate("update persona set nombre='"+persona.getNombre()+"',"
                                               + "appat='"+persona.getApellidoPaterno()
                                               +"',apmat='"+persona.getApellidoMaterno()
                                               +"',sueldobase="+persona.getSueldoBase()
                                               +",carga="+persona.getCargaFamiliar()
                                               +",movilizacion="+persona.getMovilizacion()
                                               +",colacion="+persona.getColacion()
                                               +",fecha='"+persona.getFecha()
                                               +"' where rut="+persona.getRut()+" ;");
            con.close();
        }
        catch(Exception e)
            {
                e.getStackTrace();
            }
        if (verifica!=0)
            {
                return true;
            }
        else
            {
                return false;
            }
    }


    public Liquidacion buscar(String rut)
    {
        Liquidacion liquidacion = new Liquidacion();
        try
            {
                conectar();
                ResultSet rs = st.executeQuery("select * from liquidacion "
                                                + "where rut ="+rut+";");
                while(rs.next())
                    {
                        liquidacion.setRut(rs.getString(1));
                        liquidacion.setSueldo_base(Integer.parseInt(rs.getString(2)));
                        liquidacion.setIsapre(Integer.parseInt(rs.getString(3)));
                        liquidacion.setAfp(Integer.parseInt(rs.getString(4)));
                        liquidacion.setImpuesto_unico(Integer.parseInt(rs.getString(5)));
                        liquidacion.setLiquito(Integer.parseInt(rs.getString(6)));
                    }
                con.close();
            }
        catch(Exception ex)
            {
                ex.getStackTrace();
            }
        return liquidacion;
    }   
    
    /*
    public void OrdenarAsc()
    {
        int cantidad_registros = 0;
        int persona[] = null;
        int contador = 0;
        
        try
        {
            conectar();
            
            String query = "SELECT * FROM persona";
            
            ResultSet rs = st.executeQuery(query);
            
            while(rs.next())
            {
                persona[contador] = Integer.parseInt(rs.getString("rut"));
                
                System.out.println(persona[contador]);
                
                contador++;
            }
            con.close();
               
        }
        catch(Exception ex)
        {
            ex.getStackTrace();
        }
    }
    */












    
    public int[] ordenarRutAsc()
    {
        int registro[] = null;
       // ShellSortN ssn = null;
        try
        {
            conectar();
            
            ResultSet rsCount = st.executeQuery("SELECT COUNT(*) FROM persona");
            rsCount.next();
            //JOptionPane.showMessageDialog(null, rsCount.getInt(1));
            registro = new int[rsCount.getInt(1)];
            ResultSet rs = st.executeQuery("Select * FROM persona");
            
            int contador = 0;
            while(rs.next())
            {
               
                registro[contador] = Integer.parseInt(rs.getString(1));              
                JOptionPane.showMessageDialog(null, registro[contador]);
                contador++;         
            }
            // ssn = new ShellSortN(registro);
            
            con.close();
        }
        catch(Exception ex)
        {
            ex.getStackTrace();
        }
        
        //ssn.insertSort(0, 1);
       // ssn.sort();
        
      //  registro = ssn.a;
       return registro;
    }
    
    public  int[] shellSort( int vec[]) {
    // saltos 
    for( int p = vec.length / 2; p > 0; p = p == 2 ? 1 : (int) ( p / 2.2 ) ) {
        for( int i = p; i < vec.length; i++) {
            int tmp = vec[i];
            int j;
            for(j = i; j >= p && tmp < vec[j - p]; j -= p ) {
                vec[j] = vec[j - p];
            }
            vec[j] = tmp;
        }
    }
    return vec;
 }
    
    public DefaultTableModel listar()
    {    
        Object objeto[] = null;
        try
        {
            
            conectar();        
            
            ResultSet rs = st.executeQuery("SELECT * FROM persona");
            
            while(rs.next())
            {
                objeto = new Object[10];
                
                for(int i = 0; i<10 ;i++)
                {
                    objeto[i] = rs.getString(i+1);
                }
                dt.addRow(objeto);
            }
            
          /* while(rs.next())
          {
              int contador = 0;
                objeto[contador] = rs.getString(1);
                contador++;
                //objeto.setRutVerificador(rs.getString("rutver").substring());
                
           }*/
           con.close();
        }
        catch(Exception ex)
        {
            ex.getStackTrace();
        }
        return dt;
    }
    
    public String[] sortStringAsc()
    {
        ShellSort ss = new ShellSort();
        
        try {
            conectar();
            ResultSet rsCount = st.executeQuery("SELECT COUNT(*) FROM persona");
            rsCount.next();
            apellido = new String[rsCount.getInt(1)];
           ResultSet rs = st.executeQuery("SELECT appat FROM persona");
           
           while(rs.next())
           {
               ss.add(rs.getString(1));
               //int contador = 0;
               //apellido[contador] = rs.getString(1);
               //System.out.println(apellido[contador]);
               //contador++;
           }
           con.close();
            
        } catch (Exception e) {
            e.getStackTrace();
        }
        apellido = ss.SortNames();
        return apellido;
    }
    
    public String[] sortStringDesc()
    {
       ShellSort ss = new ShellSort();
        
        try {
            conectar();
            ResultSet rsCount = st.executeQuery("SELECT COUNT(*) FROM persona");
            rsCount.next();
            apellido = new String[rsCount.getInt(1)];
           ResultSet rs = st.executeQuery("SELECT appat FROM persona");
           
           while(rs.next())
           {
               ss.add(rs.getString(1));
               //int contador = 0;
               //apellido[contador] = rs.getString(1);
               //System.out.println(apellido[contador]);
               //contador++;
           }
           con.close();
            
        } catch (Exception e) {
            e.getStackTrace();
        }
        apellido = ss.SortNames();
        return apellido; 
    }
}
